package com.zzyl.nursing.websocket;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;

/**
 * WebSocket报警消息数据模型
 * 
 * @author zzyl
 * @date 2025-07-25
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AlertMessage {

    /**
     * 消息类型: 1-报警异常, 0-解除报警
     */
    private Integer notifyType;

    /**
     * 是否广播给所有用户
     */
    private Boolean isAllConsumer;

    /**
     * 物理位置类型: 0-楼层, 2-床位
     */
    private Integer physicalLocationType;

    /**
     * 设备描述: "楼层ID,房间ID,床位ID"
     */
    private String deviceDescription;

    /**
     * 接入位置
     */
    private String accessLocation;

    /**
     * 所属产品名称
     */
    private String productName;

    /**
     * 功能名称
     */
    private String functionName;

    /**
     * 数据值
     */
    private String dataValue;

    /**
     * 设备IoT ID
     */
    private String iotId;

    /**
     * 设备名称
     */
    private String deviceName;

    /**
     * 产品Key
     */
    private String productKey;

    /**
     * 报警规则ID
     */
    private Long alertRuleId;

    /**
     * 报警原因
     */
    private String alertReason;

    /**
     * 报警等级: 1-低, 2-中, 3-高, 4-紧急
     */
    private Integer alertLevel;

    /**
     * 用户ID（如果是定向推送）
     */
    private Long userId;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 报警ID
     */
    private Long alertId;

    /**
     * 构建设备描述字符串
     * 
     * @param floorId 楼层ID
     * @param roomId 房间ID
     * @param bedId 床位ID
     * @return 格式化的设备描述
     */
    public static String buildDeviceDescription(String floorId, String roomId, String bedId) {
        return String.format("%s,%s,%s", 
            floorId != null ? floorId : "", 
            roomId != null ? roomId : "", 
            bedId != null ? bedId : ""
        );
    }

    /**
     * 创建报警异常消息
     */
    public static AlertMessage createAlertMessage(String iotId, String deviceName, 
                                                  String productName, String functionName, 
                                                  String dataValue, Integer locationType, 
                                                  String deviceDescription) {
        return AlertMessage.builder()
                .notifyType(1)  // 报警异常
                .isAllConsumer(false)
                .physicalLocationType(locationType)
                .deviceDescription(deviceDescription)
                .iotId(iotId)
                .deviceName(deviceName)
                .productName(productName)
                .functionName(functionName)
                .dataValue(dataValue)
                .createTime(LocalDateTime.now())
                .build();
    }

    /**
     * 创建解除报警消息
     */
    public static AlertMessage createResolveMessage(String deviceDescription, Integer locationType) {
        return AlertMessage.builder()
                .notifyType(0)  // 解除报警
                .isAllConsumer(false)
                .physicalLocationType(locationType)
                .deviceDescription(deviceDescription)
                .createTime(LocalDateTime.now())
                .build();
    }

    /**
     * 设置为广播消息
     */
    public AlertMessage setAsBroadcast() {
        this.isAllConsumer = true;
        return this;
    }
} 